﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using JKF.BusinessLayer.BLL;
using JKF.Substructure;

namespace JKF.Web.Sales.Consume
{
    public partial class Sales_Consume_Add : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            MODULE_NAME_EN = "SALES_CONSUME";
        }

        private static bllSALES_CONSUME salesConsumeMgr = new bllSALES_CONSUME();
        private static bllCUSTOMER customerMgr = new bllCUSTOMER();
        private static bllPRODUCT productMgr = new bllPRODUCT();
        private static bllSYS_USER sysUserMgr = new bllSYS_USER();
        private static StringBuilder stringBuilder = new StringBuilder();

        /// <summary>
        /// 配件耗材Key
        /// </summary>
        private string SalesConsumerKey
        {
            get { return P("consumeKey"); }
        }

        /// <summary>
        /// 当前待编辑对象
        /// </summary>
        public string SalesConsumeInfo
        {
            get
            {
                var consumeKey = SalesConsumerKey;
                if (consumeKey.IsGuidB())
                {
                    //build consume info
                    var consumeInfo = salesConsumeMgr.GetModel(consumeKey.ToGuid());
                    if (consumeInfo != null)
                    {
                        var customerInfo = customerMgr.GetModel(consumeInfo.CUSTOMER_KEY);
                        var product = productMgr.GetModel(consumeInfo.PRODUCT_KEY);
                        var sysUser = sysUserMgr.GetModel(consumeInfo.USER_CREATE);
                        return new
                            {
                                CONSUME_KEY = consumeInfo.CONSUME_KEY.ToString(),
                                UNIQUE_CODE = consumeInfo.UNIQUE_CODE,
                                CARD_NO = customerInfo != null ? customerInfo.CARD_NO : "",
                                CUSTOMER_Key = consumeInfo.CUSTOMER_KEY.ToString(),
                                CUSTOMER_NAME = customerInfo != null ? customerInfo.NAME : "",
                                MOBILE = customerInfo != null ? customerInfo.MOBILE : "",
                                PHONE = customerInfo != null ? customerInfo.PHONE : "",
                                PRODUCT_KEY = consumeInfo.PRODUCT_KEY.ToString(),
                                PRODUCTNAME = product != null ? product.NAME : "",
                                MODEL = product != null ? product.MODEL : "",
                                WAY_CODE = consumeInfo.WAY_CODE,
                                REGISTER_DATE = consumeInfo.REGISTER_DATE.ToShortDateLineFormat(),
                                MONEY = consumeInfo.MONEY.ToStringNoValue(),
                                ADD_POINT = consumeInfo.ADD_POINT.ToStringNoValue(),
                                STATUS = consumeInfo.STATUS == "9" ? "无效" : "有效",
                                OPERATER = sysUser == null ? "" : sysUser.REAL_NAME,
                                MEMO = consumeInfo.MEMO,
                                DATE_CREATE = consumeInfo.DATE_CREATE.ToShortDateLineFormat()
                            }.ToJson();
                    }
                }
                return "null";
            }
        }

        /// <summary>
        /// 返回配件耗材列表
        /// </summary>
        public string ConsumeProductOptions
        {
            get
            {
                var products = productMgr.GetListArray(string.Format("[type]='2' and (shop_code in(select shop_code from sys_user_shop where user_key='{0}') or shop_code is null)",
                    bllSYS_USER.CurrentUser.USER_KEY.ToString()));
                stringBuilder.Clear();
                foreach (var product in products)
                {
                    stringBuilder.AppendFormat("<option value=\"{0}\" price=\"{1}\">{2}</option>",
                        HtmlEncode(product.PRODUCT_KEY.ToString()),
                        HtmlEncode(product.PURCHASE_PRICE.ToString()),
                        HtmlEncode(product.NAME)
                    );
                }

                return stringBuilder.ToString();
            }
        }

        public string PointRate
        {
            get
            {
                var paramVl = bllSYS_PARAM.GetPointRate();
                return string.IsNullOrEmpty(paramVl) ? "1" : paramVl;
            }
        }
    }
}